<?PHP
session_start();
//$rootURL = "tusijie.com/dapei/";
$URL =  $_SERVER['SERVER_NAME'].$_SERVER[ "REQUEST_URI"];
//echo $URL;

if ($URL == "www.tusijie.com/dapei/" || $URL == "www.tusijie.com/dapei" ||
	$URL == "tusijie.com/dapei/" || $URL == "tusijie.com/dapei")
{
	header("Location:http://www.tusijie.com/dapei/index.php");
	exit;
}

preg_match_all('/.*?tusijie\.com\/dapei\/(.*)/',$URL,$m);
if (count($m) > 0 && count($m[1]) > 0 && $m[1][0] != "")
{
	$srcURL = "http://new.haibao.me/";
	$srcURL .= $m[1][0];	
	if (strstr($m[1][0],"?"))
		$srcURL .= "&host=http%3A%2F%2Fwww.tusijie.com%2Ftest%2Fhaibao.htm&amp;pagecolor=green&amp;cid=2315&amp;encoding=utf-8";
	else
		$srcURL .= "?host=http%3A%2F%2Fwww.tusijie.com%2Ftest%2Fhaibao.htm&amp;pagecolor=green&amp;cid=2315&amp;encoding=utf-8";
//	print_r($m);
//	echo $m[1][0];
}
else
{
	$srcURL = "http://new.haibao.me/?host=http%3A%2F%2Fwww.tusijie.com%2Ftest%2Fhaibao.htm&amp;pagecolor=green&amp;cid=2315&amp;encoding=utf-8";
}

if ($srcURL != "")
{
	// 缓存
	date_default_timezone_set("Asia/Chongqing");
	require_once('../public/cache.inc.php');
	$cachedir = "../cache/";
	if (isset($il_cachedir))	$cachedir = $il_cachedir;
	$cachedir .= date('Y-m-j')."/";
	$cache = new Cache($cachedir,3*3600);	// 3小时
	if (isset($_GET["refresh"]))
	{
		$url = str_replace("&refresh","",$cache->geturl());
		$url = str_replace("?refresh","",$cache->geturl());
		$cache->clean2($cache->getcacheid2($url));
	}
	
	$html = $cache->getContent();
	if ($html == "")
	{
//		echo "1";
	//	echo "<br>";
	//	echo $srcURL;
		$html = file_get_contents($srcURL);
	//	file_put_contents("1.html",$html);
		
		AdjustLink($html);
		HideSomething($html);
		AddOurs($html);
		Test($html);
		$cache->write(1, $html);
	}
	
	// 和注册用户相关，不能缓存
	// 头部
	$url = "http://www.tusijie.com/dp_src/header.php"."?" . session_name()."=".session_id();
	session_write_close();
	$opts = array(
		'http' => array(
			'method' => 'GET',
			'header' => 'Cookie: '.session_name() . "=". session_id()
		)
	);
	$context = stream_context_create($opts);
	$temp = file_get_contents($url, 0, $context);
	$html = str_replace('<div id="doc">',$temp.'<div id="doc">',$html);
	
	echo $html;
}

function AdjustLink(&$html)
{
	$html = str_replace('<a href="/','<a href="',$html);
	$html = str_replace('<a href="/p/','<a href="',$html);
	$html = str_replace('<a href="p/','<a href="',$html);
}
function HideSomething(&$html)
{
	$html = str_replace('<div id="hd">','<div id="hd" style="display:none">',$html);
	$html = str_replace('<div class="banner">','<div class="Periphery"><div class="RiHan"></div><div class="OuMei"></div><div class="MingXing"></div><div class="ReJu"></div></div><div class="banner" style="display:none">',$html);
	$html = str_replace('<div class="cate shop-recomm">','<div class="cate shop-recomm" style="display:none">',$html);
	$html = str_replace('<div class="cate shop-recomm"','<div class="cate shop-recomm" style="display:none"',$html);
//	$html = str_replace('div class="cate-title clearfix">','div class="cate-title clearfix" style="display:none">',$html);
//	$html = str_replace('<div class="cate-main clearfix">','<div class="cate-main clearfix" style="display:none">',$html);
	
	
	$html = str_replace('<p style="text-align:center;">','<p style="text-align:center;display:none">',$html);
}
function AddOurs(&$html)
{
	// 标题
	$html = str_replace('<title>图图购购</title>','<meta name="keywords" content="兔丝街,搭配,搭配技巧,服饰搭配,秋季搭配,明星搭配,搭配秀,时髦搭配,个性搭配" /> 
<meta name="description" content="兔丝街搭配秀为您时髦搭配服饰,服饰个性搭配,以及流行的日韩搭配,欧美搭配,明星搭配,还可以知道明星服饰如何搭配,热剧明星服饰怎么搭配衣服,展示您服装搭配性感的一面。" /> 
<meta name="robots" content="index, follow" /> 
<meta name="googlebot" content="index, follow" /> 
<title>兔丝街 - 看达人搭配，搭配出您的个性，崇尚唯美、精致、潮流的风格，美丽约定时尚购物~</title>',$html);

	global $URL;
	if (stristr($URL,"topic"))
	{
		$html = str_replace('</title>','兔丝街搭配秀</title>
<meta name="keywords" content="兔丝街搭配,搭配,搭配技巧,服饰搭配,秋季搭配,明星搭配,搭配秀,流行搭配,日韩搭配,欧美搭配,模特搭配" />
<meta name="description" content="兔丝街搭配秀教您服饰搭配技巧,明星搭配,搭配秀,流行搭配,日韩搭配,欧美搭配,模特搭配,打扮技巧，做时尚达人。充满卡通神秘色彩，新潮有趣，奇特好玩的时尚购物社区，是您的秘密后花园。街上的商品琳琅满目，最好看、最超值的衣服、鞋子、包包、配饰、化妆品，让您爱不惜手；奇特有趣的小商品，实用又好玩，让您惊喜又惊讶。" />
<meta name="robots" content="index, follow" /><meta name="googlebot" content="index, follow" />',$html);
	}
	else if (stristr($URL,"list_search.php"))
	{
	$html = str_replace('图图购购</title>','兔丝街搭配秀</title>
<meta name="keywords" content="兔丝街搭配,搭配,搭配技巧,服饰搭配,秋季搭配,明星搭配,搭配秀,流行搭配,日韩搭配,欧美搭配,模特搭配,打扮技巧" />
<meta name="description" content="兔丝街搭配秀教您服饰搭配技巧,明星搭配,搭配秀,流行搭配,日韩搭配,欧美搭配,模特搭配,打扮技巧，做时尚达人。充满卡通神秘色彩，新潮有趣，奇特好玩的时尚购物社区，是您的秘密后花园。街上的商品琳琅满目，最好看、最超值的衣服、鞋子、包包、配饰、化妆品，让您爱不惜手；奇特有趣的小商品，实用又好玩，让您惊喜又惊讶。" />
<meta name="robots" content="index, follow" /><meta name="googlebot" content="index, follow" />',$html);
	}

	// 样式
	$html = str_replace('</head>','<link type="text/css" rel="stylesheet" href="../css/base.css"><link type="text/css" rel="stylesheet" href="../css/dapei.css"></head>',$html);
	
	// 尾部
	$temp = file_get_contents("../networkend.php");
	$html = str_replace('</body>',$temp.'</body>',$html);
	
	$html .= '<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src=\'" + _bdhmProtocol + "hm.baidu.com/h.js%3F22b7662ec7d02c816ddfd8f6df6e5cea\' type=\'text/javascript\'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push([\'_setAccount\', \'UA-20984072-3\']);
  _gaq.push([\'_trackPageview\']);
  (function() {
    var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
    ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
    var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>';

	// 收藏和顶
	$temp = file_get_contents("../scrolltop.php");
	$html = str_replace('</body>',$temp.'</body>',$html);
}

function Test(&$html)
{
	if(isset($_GET["keyword"])){
		$html = str_replace('<body>','<body class="dapeiKeyword">',$html);		
	}else{
		$html = str_replace('<body>','<body class="dapeiIndex">',$html);
	}	
}

?>